import { type FormInstance } from 'vant';
import { ref } from 'vue';

import { deserialize } from '@/plugins/reflect-metadata';
import type { ListState } from '@/types';

import { getTransOrderList } from '../api';
import type TransOrderItem from '../models/TransOrderItem';
import TransOrderList, { TransProductQuery } from '../models/TransOrderList';

const useTransOrder = () => {
  const params = ref<TransProductQuery>(new TransProductQuery());
  const state = ref<ListState<TransOrderItem>>({
    data: [],
    total: 0,
    loading: false,
  });
  const transRef = ref<FormInstance>({} as FormInstance);

  const fetchData = async () => {
    try {
      state.value.loading = true;
      const { data } = await getTransOrderList(params.value);
      const resultData = deserialize(data, TransOrderList);
      state.value.data = resultData.items;
      state.value.total = resultData.total;
    } catch (error) {
      //
    } finally {
      state.value.loading = false;
    }
  };

  return {
    params,
    state,
    transRef,
    fetchData,
  };
};

export default useTransOrder;
